/**
 *******************************************************************************
 * @FileName  : batt_svc.c
 * @Author    : GaoQiu
 * @CreateDate: 2020-05-18
 * @Copyright : Copyright(C) GaoQiu
 *              All Rights Reserved.
 *******************************************************************************
  *
 * The information contained herein is confidential and proprietary property of
 * GaoQiu and is available under the terms of Commercial License Agreement
 * between GaoQiu and the licensee in separate contract or the terms described
 * here-in.
 *
 * This heading MUST NOT be removed from this file.
 *
 * Licensees are granted free, non-transferable use of the information in this
 * file under Mutual Non-Disclosure Agreement. NO WARRENTY of ANY KIND is provided.
 *
 *******************************************************************************
 */

#include "stack/host/att_uuid.h"
#include "stack/host/att_api.h"
#include "stack/host/dm_api.h"
#include "utils/byte_stream.h"

#include "svc_handle.h"

/*! Battery service declaration */
static const uint8_t  battSvc[] = { UINT16_TO_BYTES(ATT_UUID_BATTERY_SERVICE) };
static const uint16_t battSvcLen = sizeof(battSvc);

/*! Battery level characteristic */
static const uint8_t battLvlProp[] = {ATT_PROP_READ | ATT_PROP_NOTIFY,
		                              UINT16_TO_BYTES(BATT_LVL_HDL),
		                              UINT16_TO_BYTES(ATT_UUID_BATTERY_LEVEL)};
static const uint16_t battLvlPropLen = sizeof(battLvlProp);

/*! Battery level */
static uint8_t battLvl[] = {100};
static const uint16_t battLvlLen = sizeof(battLvl);

/*! Battery level client characteristic configuration */
static uint8_t battLvlCccd[] = {UINT16_TO_BYTES(0x0000)};
static const uint16_t battLvlCccdLen = sizeof(battLvlCccd);

static const AttsAttr_t battAttrs[] = {
	{
		primSvcUuid,
		(uint8_t *)battSvc,
		(uint16_t *)&battSvcLen,
		sizeof(battSvc),
		0,
		ATTS_PERMIT_READ,
	},

	{
		charDecUuid,
		(uint8_t *)battLvlProp,
		(uint16_t *)&battLvlPropLen,
		sizeof(battLvlProp),
		0,
		ATTS_PERMIT_READ,
	},

	{
		battLevelUuid,
		(uint8_t *)battLvl,
		(uint16_t *)&battLvlLen,
		sizeof(battLvl),
		ATTS_SET_READ_CBACK,
		ATTS_PERMIT_READ,
	},

	{
		clientCharCfgUuid,
		(uint8_t *)battLvlCccd,
		(uint16_t *)&battLvlCccdLen,
		sizeof(battLvlCccd),
		ATTS_SET_CCC,
		ATTS_PERMIT_READ|ATTS_PERMIT_WRITE,
	},
};


AttState_t APP_BatteryAttReadCBack(uint16_t connHandle, uint16_t attHandle, uint8_t opcode,
                                   uint16_t offset, AttsAttr_t* pAttr)
{
	pAttr->pValue[0] = 100;//++;
	return ATT_SUCCESS;
}

AttsGroup_t battGrp = {
	NULL,
	(AttsAttr_t *)battAttrs,
	APP_BatteryAttReadCBack,
	NULL,
	BATT_START_HDL,
	BATT_END_HDL,
};

uint8_t BAS_UpdBattLevel(uint16_t connHandle)
{
#if 1
	if(ATTS_CccEnabled(connHandle, BATT_LVL_CCC_IDX)){
		if(battLvl[0] > 80){
			battLvl[0]--;
		}
		else{
			battLvl[0] = 100;
		}
		uint8_t res = ATTS_ValueNotify(connHandle, BATT_LVL_HDL, battLvl, 1);
		return res;
	}
#else //[Panchip]
	return ATTS_ValueNotify(connHandle, BATT_LVL_HDL, pData, len);
#endif

	return BLE_ERR_ATT_SERVER_NOTIFY_NOT_ENABLED;
}




